<?php 
class WithdrawAction extends AdminPublicAction{
	/*======================================================================
	 信息列表
	 ======================================================================================*/
	public function index(){
	
		if((isset($_GET['sort'] ) && !empty($_GET['sort'])) && (isset($_GET['updown'] ) && !empty($_GET['updown']))){
			$sort   = I('o.sort');
			$upDown = I('updown');
		}else{
			$_GET['sort'] = $sort = "member_withdraw_id";
			$_GET['updown'] = $upDown = "desc";
	
		}
	
		$keyword = I('keyword','');
	
		$flag = I('flag',-1);
	
		 //關鍵字查詢
		if(tep_not_null($keyword)){
			$where['name']  = array('like','%'.$keyword.'%');
			$where['member_id']  = array('like','%'.$keyword.'%');
			$where['_logic'] = 'or';
			$map['_complex'] = $where; 
			//$where['m.name'] = $keyword;
		} 
		//省份城市筛选
		/* $map = get_map_city_id($map);
			$this->city_id = $map['city_id'];
			$this->prov_id = $map['prov_id'];		 */
	
		//篩選狀態
		if($flag >= 0 ) $map['flag'] = $flag;
	
		//$map['language_id'] = (int)L('lan_id');
	
		import("ORG.Util.Page");
		$page = 10;
		$count = D('MemberMoneylog')->getWcount($map);
		$Page = new Page($count, $page);
		$infoList = D('MemberMoneylog')->GetWithdraw($map,$sort.' '.$upDown,$Page->firstRow. ',' . $Page->listRows);//getAll($map,$Page->firstRow. ',' . $Page->listRows,$sort.' '.$upDown);
		//var_dump($infoList);die;
		if (S("success") != "")
		{
			$this->assign("msg", '<div class="success">'.S("success").'</div>');
			S('success',NULL);
		}
			
		if (S("err") != "")
		{
			$this->assign("msg", '<div class="err">'.S("err").'</div>');
			S('err',NULL);
		}
	
		//$this->category = $category;
		$this->flag = $flag;
		$this->keyword = $keyword;
		//$this->cate = $cate;
		$this->page = $Page->show();
		$this->infoList = $infoList;
		$this->li1 = 'current';
		$this->left_order = 'current';
		$this->display();
	}
	
	//###########################################//
	//确认订单       ，即查看在线邮件，若买家收到邮件长时间未回复，则让订单完成，将金额打给卖家，并更改订单状态                                                                                                          //
	//###########################################//
	public function confirm(){
		$editId = I('editId');
		if(!$editId){
			$this->redirect('index');
		}
		//$orderDetail = M('member_withdraw')->where(array('member_withdraw_id'=>$editId))->find();
		$moneylog = M('member_moneylog')->where(array('member_withdraw_id'=>$editId))->find();
		$flag = 1;//处理金额体现给客户账户
		$data['affect_money'] = $moneylog['affect_money'];
		$data['active_money'] = $moneylog['active_money'];
		$data['freeze_money'] = $moneylog['freeze_money'] - $moneylog['affect_money'];
		$data['uid'] = $moneylog['uid'];
		$data['money_type'] = 9;
		$data['moneylog_affect_date'] = date('Y-m-d H:i:s', time());
		$data['member_withdraw_id'] = $editId;
		$data['u_ip'] = getip();
		$data['comment'] = '管理员同意提现，将金额打给会员微信账户';
		$result = M('member_moneylog')->add($data);
		if($result){
			M('member_moneylog')->where(array('member_moneylog_id'=>$moneylog['member_moneylog_id']))->setField('freeze_flag', 1);
			$data1['flag'] = 1;
			$result = M('member_withdraw')->where(array('member_withdraw_id'=>$editId))->save($data1);
			if($result){
				S('success','确认提现成功!');
				$this->redirect('index');
			}else{
				S('err','确认提现失败!');
				$this->redirect('index');
			}
		}else{
			S('err','确认提现失败!');
			$this->redirect('index');
		}
	
	
	
	}
	
	//###########################################
	//拒绝提现。
	//###########################################
	public function cancel(){
		$editId = I('editId');
		if(!$editId){
			$this->redirect('index');
		}
		//$orderDetail = M('order')->where(array('order_id'=>$editId))->find();
		$moneylogdetail = M('member_moneylog')->where(array('member_withdraw_id'=>$editId))->find();
		$data['active_money'] =$moneylogdetail['active_money']+$moneylogdetail['affect_money'];
		$data['freeze_money'] = $moneylogdetail['freeze_money']-$moneylogdetail['affect_money'];
		
		$data['uid'] = $moneylogdetail['uid'];
		$data['money_type'] = 10;
		$data['affect_money'] =$moneylogdetail['affect_money'];
		$data['moneylog_affect_date'] = date('Y-m-d H:i:s',time());
		$data['member_withdraw_id'] = $editId;
		$data['u_ip'] =  getip();
		$data['comment'] = '管理人员拒绝提现，金额转回活动金额';
		//var_dump($data1);die;
		$result = M('member_moneylog')->add($data);
		if($result){//冻结转回活动金额成功，设置解冻状态及提现状态
			$bmoneylogdetail = M('member_moneylog')->where(array('member_moneylog_id'=>$moneylogdetail['member_moneylog_id']))->setField('freeze_flag', 1);
			$result = M('member_withdraw')->where(array('member_withdraw_id'=>$editId))->setField('flag',2);			
			if($result){
				S('success','拒绝提现处理成功!');
				$this->redirect('index');
			}else{
				S('err','拒绝提现处理失败!');
				$this->redirect('index');
			}
	
		}else{
			S('err','拒绝提现处理失败!');
			$this->redirect('index');
		}	
		
	}
}
?>